<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand"/>
    <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
    <title>课程-任务-发布（当堂测试、课后作业、实验任务）</title>

    <%@ include file="/common/commonLib.jsp" %>
    <link rel="stylesheet" type="text/css" href="<c:url value='/css/taskPublish.css'/>"/>
    <link rel="stylesheet" type="text/css" href="<c:url value='/css/common-main.css'/>"/>
    <link rel="stylesheet" type="text/css" href="/CourseSupport/css/checkboxcss/inserthtml.com.radios.css">
    <script src="<c:url value='/js/taskOtherPublish.js'/>" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="container-fluid main">
    <div class="row">
        <div class="col-md-12">
            <%@include file="/common/header.jsp" %>
        </div>
        <div class="col-md-12">
            <%@include file="common/courseHeader.jsp" %>
        </div>
        <div class="col-md-12">
            <div class="row">
                <div class="col-md-3">
                    <%@include file="common/courseLeft.jsp" %>
                </div>
                <div class="col-md-9">
                    <div class="row">
                        <div class="col-md-12">
                            <%@include file="common/courseNavbar.jsp" %>
                        </div>
                        <div class="col-md-12">
                            <div class="tab-nav">
                                <a id="other-task-pub-href1" class="front-nav">任务首页
                                </a>&nbsp;>&nbsp;<a id="other-task-pub-href2" class="front-nav">任务详情
                            </a>&nbsp;>&nbsp;<span class="back-nav">任务分配情况</span>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <input type="button" id="new-other-task-publish" class="btn btn-primary" value="分配任务"
                                   title="选择班级、课堂、截止日期，发布任务">
                        </div>
                        <div class="col-md-12" style="margin-top: 2rem;">
                            <label for="">任务分配记录</label>
                            <!-- 课堂提问发布记录 -->
                            <ul id="task-pub-record-other">

                            </ul>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 添加任务分配对象 -->
<div class="row" id="add-other-publish-object">

    <div class="col-md-11">
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <div class="row">
                        <div class="col-xs-2">
                            <label for="">选择班级</label>
                        </div>
                        <div class="col-xs-4">
                            <select class="form-control" id="classList" onchange="selectTaskOtherClass(this)">

                            </select>
                        </div>
                        <div class="col-xs-2">
                            <label for="">理论课/实验课</label>
                        </div>
                        <div class="col-xs-4">
                            <input type="checkbox" id="checkbox">
                            <label for="checkbox"></label>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-12">
                <div class="form-group">
                    <div class="row">
                        <div class="col-xs-2">
                            <label for="">选择课堂</label>
                        </div>
                        <div class="col-xs-10">
                            <div class="publish-classroom-ready">

                                <ul id="lessonListOther">

                                </ul>
                            </div>
                            <div class="page-content">
                                <nav class="pagination" role="navigation">
                                    <a class="last-page" id="lastPage">
                                        <i class="fa fa-angle-left"></i>
                                    </a>
                                    <span class="page-number">
											第<span id="pageNow"></span>页/共<span id="pageNum"></span>页
										</span>
                                    <a class="next-page" id="nextPage">
                                        <i class="fa fa-angle-right"></i>
                                    </a>
                                </nav>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-12">
                <div class="form-group">
                    <div class="row">
                        <div class="col-xs-2">
                            <label for="">已选课堂</label>
                        </div>
                        <div class="col-xs-10">
                            <div class="publish-classroom-has">

                                <ul id="chooseListOther">
                                </ul>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-12" style="text-align:center;margin-bottom:4rem;">
                <button type="button" id="taskOtherPublishBtn" class="btn btn-primary">发布</button>
            </div>
        </div>
<span style=""></span>

    </div>
    <div class="col-md-1"></div>
</div>

<!-- 某个发布记录详情编辑 -->


<!-- 选择截止时间 -->
<div class="modal fade" id="setTaskInfo">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" style="display:inline;float:left;">任务发布信息编辑</h4>
                <button type="button" class="close" data-dismiss="modal" style="display:inline;float:right;">&times;
                </button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="task-list-id-hidden">
                <div class="row">
                    <div class="col-xs-1"></div>
                    <div class="col-xs-10">
                        <div class="row">
                            <div class="col-xs-12">
                                <div class="form-group">
                                    <div class="row">
                                        <div class="col-xs-3"><label>截止时间</label></div>
                                        <div class="col-xs-9">
                                            <input type="text" id="updateDeadline" class="form-control">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-xs-12">
                                <div class="form-group">
                                    <div class="row">
                                        <div class="col-xs-4"><label>是否公布答案</label></div>
                                        <div class="col-xs-8">
                                            <input type="radio" name="pub-answer" value="YES">是
                                            <input type="radio" name="pub-answer" value="NO">否
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-xs-12">
                                <div class="form-group">
                                    <div class="row">
                                        <div class="col-xs-4"><label>发布</label></div>
                                        <div class="col-xs-8">
                                            <input type="radio" name="pub-task"  value="YES">立即发布
                                            <input type="radio" name="pub-task" value="NO">预发布
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div id="publish-time" class="col-xs-12">
                                <div class="form-group">
                                    <div class="row">
                                        <div class="col-xs-3"><label>任务公布时间</label></div>
                                        <div class="col-xs-9">
                                            <input type="text" id="updatePublishtime" class="form-control">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="task-publish-modal">确定</button>
            </div>
        </div>
    </div>
</div>
<%@include file="/common/footer.jsp" %>
</body>
<script>
    var data =${data};
    var classs = [];
    var lessons = [];
    var chooses = [];
    var courseId;
    var taskId;
    $(document).ready(function () {
        if (data.status != "success") {
            alert(data.message);
            return;
        }
        $("#task").css("color", "#00b43c");
        courseId = data.data.courseId;
        changeUrl(data.data.courseId);
        taskId = data.data.taskId;
        $("#taskOtherPublishBtn").click(publishTask);
        fetchCourse(data.data.courseId);
        initAssignList();

        $("#other-task-pub-href1").attr("href", CONTEXT_PATH + "task/list?courseId=" + data.data.courseId);
        $("#other-task-pub-href2").attr("href", CONTEXT_PATH + "task/detail?id=" + data.data.taskId);
    });

    var selectTaskOtherClass = function (value) {
        var selectedOption = value.options[value.selectedIndex];
        getLessons(selectedOption.value);
    }

    $(function () {
        $(".publish-edit").click(function () {
            var index = $(this).siblings("input[name='edit-task-id']").val();

            $("#setTaskInfo").modal("show");
            var item = data.data.list[index];
            console.log(item);
            $("input[id='updateDeadline']").val(item.deadline);
            $("input[id='task-list-id-hidden']").val(item.id);
            $("input[id='updatePublishtime']").val(item.publishDatetime);
            if (item.isVisible == "YES") {
                $("input[name='pub-task']").get(0).checked = true;
                $("#publish-time").hide();
                $("#publish-time").val("");
            } else {
                $("input[name='pub-task']").get(1).checked = true;
                $("input[id='updatePublishtime']").val(item.publishDatetime);
                $("#publish-time").show();
            }
            if (item.isAnswerVisible == "YES") {
                $("input[name='pub-answer']").get(0).checked = true;

            } else {
                $("input[name='pub-answer']").get(1).checked = true;

            }
            $("#updateDeadline").focus(function () {
                $("input[id='updateDeadline']").attr("type", "datetime-local");
            });
            $("#updatePublishtime").focus(function () {
                $("input[id='updatePublishtime']").attr("type", "datetime-local");
            });
        });
        /*点击编辑框“确定”按钮*/
        $("#task-publish-modal").click(function () {
            var str0 = $("input[id='task-list-id-hidden']").val();
            updateDeadLine(str0);
            $("#setTaskInfo").modal("hide");
        });

        $("#checkbox").click(function () {
            getLessons($("#classList option:selected").val());
        });
        //点击预发布显示公布时间输入框
        $('input[name="pub-task"]').click(function () {
            if($('input[name="pub-task"]:checked').val()=="NO"){
                $("#publish-time").show();
            }else {
                $("#publish-time").hide();
                $("input[id='updatePublishtime']").val("");

            }
        })
    });

    var getIsVisible = function (isVisible) {
        if (isVisible == "YES") {
            return "已发布";
        } else {
            return "预发布";
        }
    }

    var getIsAnswerVisible = function (isAnswerVisible) {
        if (isAnswerVisible == "YES") {
            return "答案已公布";
        } else {
            return "答案未公布";
        }
    }

    var initAssignList = function () {
        // console.log("data111:"+JSON.stringify(data))
        var list = $("#task-pub-record-other");
        list.empty();
        if (data.data.list.length <= 0) {
            var s = "<li title=\"点击(分配任务)按钮进行分配\">（无分配记录）</li>";
            list.append(s);
        } else {
            for (var i = 0; i < data.data.list.length; i++) {
                var item = data.data.list[i];
                var s = "<li>";
                s += "<a href=\"<c:url value='/taskSubmitResult/mtlistData?assignId="+item.id+"'/>\"><span title=\"" + item.className + ">课堂为" + item.beginDate + "-" + item.endDate + ">地点:" + item.location + ">截止时间:" + item.deadline + ">状态:" + getIsVisible(item.isVisible) + ">" + getIsAnswerVisible(item.isAnswerVisible) + "\">" + item.className + ">课堂为" + item.beginDate + "-" + item.endDate + ">地点:" + item.location + ">截止时间:" + item.deadline + ">状态:" + getIsVisible(item.isVisible) + ">" + getIsAnswerVisible(item.isAnswerVisible) + "</span></a>";
                s += "<input type=\"button\" class=\"btn btn-info publish-edit\" value=\"编辑\" title=\"修改截止日期,发布状态和答案公布状态\">";

                // s += "<button onclick=\"delete1(" + item.id + ") \" class=\"btn btn-danger\" style='float: left; width: 10%'>删除</button>";
                s += "<input type=\"button\" onclick=\"delete1(" + item.id + ") \" class=\"btn btn-danger publish-edit\" value=\"删除\">";

                s += "<input type=\"hidden\" name= \"edit-task-id\" value=\"" + i + "\">";
                s += "</li>";

                list.append(s);
            }
        }

    };

    function delete1(id) {
        console.log("id:::" + id);
        if (window.confirm("您确定要删除吗?")) {
            $.post(CONTEXT_PATH + "taskAssignment/ajaxDelete", "id=" + id, function (data) {
                if (data.status == "success") {
                    layer.msg("删除成功");
                    location.reload();
                } else {
                    layer.msg("删除失败");
                }
            });
        }

    }

    var getClassList = function () {
        var json = {
            courseId: data.data.courseId,
            classType: "CLASS"
        }
        $.get(CONTEXT_PATH + "class/ajaxListData", {
            data: JSON.stringify(json)
        }, function (data) {
            if (data.status == "success") {
                initClassList(data.data.list);
            } else {
                alert(data.message);
            }
        });
    }

    var initClassList = function (list) {
        classs = list;
        var classList = $("select[id='classList']");
        classList.empty();
        for (var i = 0; i < list.length; i++) {
            if (i == 0) {
                classList.append("<option selected=\"selected\" value='" + list[i].id + "'>" + list[i].name + "</option>");
                getLessons(list[i].id);
            } else {
                classList.append("<option value='" + list[i].id + "'>" + list[i].name + "</option>");
            }
        }
    }

    var getLessons = function (classId) {
        if (isEmpty(classId)) {
            classId = $("#classList option:selected").val();
        }
        var selectLessonType = "THEORY";
        if ($("#checkbox").is(":checked")) {
            selectLessonType = "LAB";
        }
        var json = {
            classId: classId,
            lessonType: selectLessonType,
            taskId: data.data.taskId
        }
        $.get(CONTEXT_PATH + "taskAssignment/assignLesson", {
            data: JSON.stringify(json)
        }, function (data) {
            if (data.status == "success") {
                initLesson(data.data.lessons);
            } else {
                alert(data.message);
            }
        });
    }
    var initLesson = function (list) {
        lessons = list;
        var begin = 0;
        var end = 10;
        var pageNow = 1;
        var pageNum;
        if (list.length < 10) {
            end = list.length;
            pageNum = 1;
            pageNow = 1;
            $(".page-content").hide();
        } else {
            $(".page-content").show();
            pageNum = Math.ceil(list.length / 10);
            begin = (pageNow - 1) * 10;
        }
        var pageNow1 = $("#pageNow").text();
        $("#lastPage").click(function () {
            if (pageNow1 == 1 || pageNow == 1) {
                pageNow = 1;
                begin = 0;
                layer.msg('已经是第一页啦！');
            } else {
                pageNow--;
                begin = (pageNow - 1) * 10;
            }
            end = pageNow * 10;
            $("#pageNow").text(pageNow);
            $("#pageNum").text(pageNum);
            pageList(list, begin, end);
        });
        $("#nextPage").click(function () {
            if (pageNow1 == pageNum || pageNow == pageNum) {
                pageNow = pageNum;
                end = list.length;
                layer.msg('已经是最后一页啦！');
            } else {
                pageNow++;
                if (pageNow == pageNum) {
                    end = list.length;
                } else {
                    end = pageNow * 10;
                }
            }
            begin = (pageNow - 1) * 10;
            $("#pageNow").text(pageNow);
            $("#pageNum").text(pageNum);
            pageList(list, begin, end);
        });
        $("#pageNow").text(pageNow);
        $("#pageNum").text(pageNum);
        pageList(list, begin, end);
    }

    var pageList = function (list, begin, end) {
        var lessonList = $("#lessonListOther");
        lessonList.empty();
        if (list.length <= 0) {
            var s = "<li><span>已有的课堂都分配过该任务了</span></li>";
            lessonList.append(s);
        } else {
            for (var i = begin; i < end; i++) {
                var s = "";
                if (contain(list[i])) {
                    s += "<li>";
                    s += "<span title=\"课堂:" + list[i].beginDate + "--" + list[i].endDate + ">" + list[i].location + "\">课堂:" + list[i].beginDate + "--" + list[i].endDate + ">" + list[i].location + "</span>";
                    s += "<input type=\"button\" disabled=\"disabled\" class=\"btn btn-primary add-lesson\" value=\"添加\">";
                    s += "</li>";
                    lessonList.append(s);
                } else {
                    s += "<li>";
                    s += "<span title=\"课堂:" + list[i].beginDate + "--" + list[i].endDate + ">" + list[i].location + "\">课堂:" + list[i].beginDate + "--" + list[i].endDate + ">" + list[i].location + "</span>";
                    s += "<input type=\"button\" class=\"btn btn-primary add-lesson\" onclick=\"initChooseList(" + i + ",this)\" value=\"添加\">";
                    s += "</li>";
                    lessonList.append(s);
                }
            }
        }
    }

    var initChooseList = function (i, obj) {
        var chooseList = $("#chooseListOther");
        var item = lessons[i];

        var s = "";
        s += "<li>";
        s += "<span title=\"" + item.beginDate + "--" + item.endDate + "  " + item.location + "\">" + item.beginDate + "--" + item.endDate + "  " + item.location + "</span>";
        s += "<p class=\"radio-cho\" ><input type=\"radio\" name=\"pub" + chooses.length + "\" value=\"NO\" onclick=\"isPublish(" + chooses.length + ")\" checked >预发布</p>";
        s += "<p class=\"radio-cho\" ><input type=\"radio\" name=\"pub" + chooses.length + "\" value=\"YES\" onclick=\"isPublish(" + chooses.length + ")\">立即发布</p>";

        s += "<label>截止时间：</label>";
        s += "<input type=\"datetime-local\" name=\"deadline\" class=\"form-control\" >";
        s += "<label name=\"publishTimeLabel" + chooses.length + "\">公开时间：</label>";
        s += "<input type=\"datetime-local\" name=\"publishTime" + chooses.length + "\" class=\"form-control\" >";
        s += "<input type=\"button\" class=\"btn btn-danger del-lesson\" onclick=\"deleteChooseLesson(" + i + ",this)\" value=\"删除\">";
        s += "</li>";

        chooseList.append(s);
        chooses.push(item);
        $(obj).attr("disabled", "disabled");
    }

    function isPublish(num){
        console.log("函数执行");
            if($("input[name='pub" + num + "']:checked").val()=="NO"){
                console.log("预发布");
                $("input[name='publishTime" + num + "']").show();
                $("label[name='publishTimeLabel" + num + "']").show();
            }
            else {
                console.log("发布");
                $("input[name='publishTime" + num + "']").hide();
                $("label[name='publishTimeLabel" + num + "']").hide();
                $("input[name='publishTime" + num + "']").val("");
            }
    }


    var deleteChooseLesson = function (i, obj) {
        var item = lessons[i];
        $(obj).parent("li").remove();
        chooses.pop(item);
        initLesson(lessons);
    }
    var contain = function (item) {
        for (var i = 0; i < chooses.length; i++) {
            if (chooses[i].id == item.id) {
                return true;
            }
        }
        return false;
    }


    var publishTask = function () {
        var param = [];
        if (chooses.length <= 0) {
            alert("请选择待发布课堂");
            return;
        }
        var deadlines = $("input[name='deadline']");
        // var publishTimes = $("input[name='publishTime']");
        for (var i = 0; i < chooses.length; i++) {
            var isVisible = $("input[name='pub" + i + "']:checked").val();
            if (isEmpty(isVisible)) {
                isVisible = "NO";
            }
            var deadline = deadlines[i].value;
            if (isEmpty(deadline)) {
                alert("请选择截止日期！");
                return false;
            }

            //任务预发布 选择任务公布时间
            // var publishTime = publishTimes[i].value;
            var publishTime = $("input[name='publishTime" + i + "']").val();
            if(isVisible=="NO"){
                if (isEmpty(publishTime)) {
                    alert("请选择任务公布日期！");
                    return false;
                }
                publishTime = parseDate(publishTime);
            } else {
                publishTime = "";
            }
            deadline = parseDate(deadline);
            var item = {
                lessonId: chooses[i].id,
                isVisible: isVisible,
                deadline: deadline,
                publishDatetime:publishTime
            };
            param.push(item);
        }

        var json = {
            taskId: taskId,
            items: param
        };

        $.post(CONTEXT_PATH + "taskAssignment/ajaxBatchCreate", {
            data: JSON.stringify(json)
        }, function (data) {
            if (data.status == "success") {
                window.location.reload();
            } else {
                alert(data.message);
            }
        });
    };

    var isEmpty = function (s) {
        if (s == null || s == "" || s == undefined) {
            return true;
        }
        return false;
    }

    var showAnswer = function (id, isVisible) {
        var json = {
            id: id,
            isVisible: isVisible
        };
        $.post(CONTEXT_PATH + "taskAssignment/ajaxUpdateAnswer", {
            data: JSON.stringify(json)
        }, function (data) {
            if (data.status == "success") {
                window.location.reload();
            } else {
                alert(data.message);
            }
        });
    }

    var updateDeadLine = function (id) {
        var deadline = $("#updateDeadline").val();
        deadline = parseDate(deadline);
        var publishTime = $("#updatePublishtime").val();
        // publishDatetime = parseDate(publishDatetime);
        var isVisible = $("input[name='pub-task']:checked").val();
        var isAnswerVisible = $("input[name='pub-answer']:checked").val();

        if(isVisible=="NO"){
            if (isEmpty(publishTime)) {
                alert("请选择任务公布日期！");
                return false;
            }
            publishTime = parseDate(publishTime);
        } else {
            publishTime = "";
        }
        var json = {
            id: id,
            deadline: deadline,
            isVisible: isVisible,
            isAnswerVisible: isAnswerVisible,
            publishDatetime:publishTime
        };
        console.log("111")
        console.log(json)
        $.post(CONTEXT_PATH + "taskAssignment/ajaxUpdateAssignStatus", {
            data: JSON.stringify(json)
        }, function (data) {
            if (data.status == "success") {
                window.location.reload();
            } else {
                alert(data.message);
            }
        });
    }

</script>
</html>
